/**
 * @author Jiange
 * @create 2021-07-15-22:58
 */
public class SyncronizedTest {
    /**
     * 修饰代码块和方法
     */
    public static void main(String[] args) {
        Ticket ticket = new Ticket();

        new Thread(()->{
            for (int i = 0; i < 1000; i++) {
                ticket.sale();
            }
        },"AA").start();
        new Thread(()->{
            for (int i = 0; i < 1000; i++) {
                ticket.sale();
            }
        },"BB").start();
        new Thread(()->{
            for (int i = 0; i < 1000; i++) {
                ticket.sale();
            }
        },"CC").start();
    }
}
class Ticket{
    private int number = 1000;

    public synchronized void sale(){
        if(number>0){
            System.out.println(Thread.currentThread().getName()+"--"+number);
            number--;
        }
    }
}
